package com.sky.testmodule.controller.redis;

import com.sky.testmodule.constant.TestConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhiwei Liao
 * @version 1.0
 * @Description
 * @Date 2021/9/22 16:26
 */
@RequestMapping("/set")
@RestController
public class SetController {

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/setCache")
    public void setCache(){
        //根据TestConstant.KEY_TEST获取Set中的所有值
        redisTemplate.opsForSet().members(TestConstant.KEY_TEST);
        //根据new Object()从一个set中查询,是否存在
        redisTemplate.opsForSet().isMember(TestConstant.KEY_TEST, new Object());
        //将数据放入set缓存
        redisTemplate.opsForSet().add(TestConstant.KEY_TEST, new Object());
        //获取SET缓存的长度
        redisTemplate.opsForSet().size(TestConstant.KEY_TEST);
        //移除指定TestConstant.KEY_TEST的缓存
        redisTemplate.opsForSet().remove(TestConstant.KEY_TEST, new Object());
        //获取二个集合的差集
        redisTemplate.opsForSet().difference(TestConstant.KEY_TEST, TestConstant.OBJECT_CACHE_KEY);
        //获取二个集合的交集
        redisTemplate.opsForSet().intersect(TestConstant.KEY_TEST, TestConstant.OBJECT_CACHE_KEY);
        //获取二个集合的并集
        redisTemplate.opsForSet().union(TestConstant.KEY_TEST, TestConstant.OBJECT_CACHE_KEY);
        //将求出来的差集元素保存
        redisTemplate.opsForSet().differenceAndStore(TestConstant.KEY_TEST, TestConstant.OBJECT_CACHE_KEY,TestConstant.KEY_OTHER);
        //将求出来的交集元素保存
        redisTemplate.opsForSet().intersectAndStore(TestConstant.KEY_TEST, TestConstant.OBJECT_CACHE_KEY,TestConstant.KEY_OTHER);
        //将求出来的并集元素保存
        redisTemplate.opsForSet().unionAndStore(TestConstant.KEY_TEST, TestConstant.OBJECT_CACHE_KEY,TestConstant.KEY_OTHER);
        //随机获取变量中的元素
        redisTemplate.opsForSet().randomMember(TestConstant.KEY_TEST);
        //随机获取变量中指定个数的元素
        redisTemplate.opsForSet().randomMembers(TestConstant.KEY_TEST,2);
        //转移变量的元素值到目的变量
        redisTemplate.opsForSet().move(TestConstant.OBJECT_CACHE_KEY,TestConstant.OBJECT_CACHE_VALUE,TestConstant.KEY_OTHER);
        //弹出变量中的元素,剩余元素就少了弹出的元素
        redisTemplate.opsForSet().pop(TestConstant.OBJECT_CACHE_KEY);
        //获取去重的随机元素
        redisTemplate.opsForSet().distinctRandomMembers(TestConstant.OBJECT_CACHE_KEY,2);
        //匹配获取键值对，ScanOptions.NONE为获取全部键值对；ScanOptions.scanOptions().match("C").build()匹配获取键位map1的键值对,不能模糊匹配。
        redisTemplate.opsForSet().scan(TestConstant.OBJECT_CACHE_KEY, ScanOptions.NONE);
        Cursor cursor = redisTemplate.opsForSet().scan(TestConstant.OBJECT_CACHE_KEY,
                ScanOptions.scanOptions().match(TestConstant.OBJECT_CACHE_KEY).build());
        while (cursor.hasNext()){
             Object object = cursor.next();
             System.out.println("通过scan(K key, ScanOptions options)方法获取匹配的值:" + object);
        }
    }
}
